import ToolActionApi from "@/tool/http/tool-action.js";

let apiDict = ToolActionApi.init(
  "sys_dict",
  "cc3ac1352b104abfb86da5c351f8f68c"
);

const rule = {
  queryParams: [],
  formOnChange: (watchForm) => {
    if (!watchForm._address) {
      watchForm._address = [
        watchForm.province,
        watchForm.city,
        watchForm.county,
      ];
    } else {
      watchForm.province = watchForm._address[0] || "";
      watchForm.city = watchForm._address[1] || "";
      watchForm.county = watchForm._address[2] || "";
    }
    return [
      {
        name: "_address",
        memo: "行政区域",
        type: 171,
      },
    ];
  },
  pageConfig: {},
  metaConfig: {
    province: {
      hidden: true,
    },
    city: {
      hidden: true,
    },
    county: {
      hidden: true,
    },
    id: {
      hidden: true,
    },
    tenant_id: {
      hidden: true,
    },
    licence: {
      hidden: true,
    },
    created_at: {
      hidden: true,
    },
    updated_at: {
      hidden: true,
    },
    deleted_at: {
      hidden: true,
    },
    permit_licence: {
      hidden: true,
    },
    legal_person: {
      hidden: true,
    },
    address_detail: {
      type: 1,
    },
    focus_on: {
      dictList: [
        {
          name: "是",
          val: 1,
        },
        {
          name: "否",
          val: 0,
        },
      ],
    },
  },
};

export default async function (metadata) {
  rule.metaConfig.industry = {
    dictList: await getDictByType("industry"),
  };
  rule.metaConfig.erp_type = {
    dictList: await getDictByType("erp_type"),
  };
  rule.metaConfig.scale = {
    dictList: await getDictByType("scale"),
  };
  rule.metaConfig.subjection = {
    dictList: await getDictByType("subjection"),
  };
  return rule;
}

async function getDictByType(type) {
  let res = await apiDict.list({
    rules: {
      dict_type__eq: type,
    },
  });
  return res.map((item) => ({
    name: item.dict_name,
    val: item.dict_value,
  }));
}
